<?php
/**
 * Game Lobby Controller
 */
class GameLobby extends controller{

	public function indexAction(){
		self::joinMainLobbyAction();
	}
	
	/**
	 * redirects to lobby-main.phtml view
	 *  
	 */
	public function joinMainLobbyAction(){
		//$viewVariables = array("targetBattleId"=>1);
		//return self::loadView("site/lobby-list.phtml", $viewVariables);
		return self::loadView("site/lobby-main.phtml");
	}
	
	/**
	 * redirects to lobby-staging.phtml view
	 *
	 * @return unknown
	 */
	public function joinStagingAreaAction(){
		//$viewVariables = array("targetBattleId"=>1);
		//return self::loadView("site/lobby-list.phtml", $viewVariables);
		return self::loadView("site/lobby-staging.phtml");
	}
	
	/**
	 * Creates a new battle instance with the given BattleName, HostPlayerId & Password(Optional)
	 * Then forwards the host to the staging page
	 *
	 * @param array $request ['B'=BattleId, 'Bn'=BattleName varchar(16), 'P'=PlayerId, 'Pwd'=Password varchar(16) if required (otherwise pass an empty string!)]
	 * @return NONE. Forwards host to staging page
	 */
	public static function stageAsHostAction(array $request){
		require_once(DirectoryManager::$pathUnitTests."/defaults/Default_Battle.php");
		require_once(DirectoryManager::$pathModels."/battle/BattleCommand.php");
		
		//=== USING UNIT TEST INSTEAD FOR NOW ===========================
		//Create a New Battle Instance
		//$return = BattleCommand::createBattleInstance($request);
		$return = Default_Battle::createDefaultBattleInstanceForHost($request);
		$newBattleId = $return['B'];
		$newTimeStamp = $return['ts'];
		
		//TODO Add unit deployment phase instead of UnitTests
		//=== USING UNIT TEST INSTEAD FOR NOW ===========================
		
		// Redirect to staging area based on new battleId
		//self::redirect("site.php?c=GameLobby&a=lobby-staging&B=".$newBattleId."&Bn=".$request['Bn']);
		//TEMPORARILY INCLUDES PLAYERID (This will later be provided by the Session!)
		self::redirect("site.php?c=GameLobby&a=joinStagingArea&B=".$newBattleId."&Bn=".$request['Bn']."&P=".$request['P']."&ts=".$newTimeStamp);
	}

	/**
	 * Player attempts to join target Battle and is forwarded to Staging OR back MainLobby with error code
	 * 
	 * @param array $request ('B'=TargetBattleId, 'P'=PlayerId, 'Pwd'=Password if required, NULL otherwise)
	 * @return NO RETURN, instead forwards player with url arguments...
	 * Success => self::redirect("site.php?c=GameLobby&a=lobby-staging&B=".$return["B"]."&Bn=".$return["Bn"]."&P=".$playerId."&ts=".$return["ts"]);
	 * Failure => self::redirect("site.php?c=GameLobby&a=lobby-list&E=-".$success);
	 * Where E = error code -1 to -4 (See in line comments)
	 */
	public static function stageAsJoinerAction(array $request){
		require_once(DirectoryManager::$pathModels."/Lobby/LobbyCommand.php");
		require_once(DirectoryManager::$pathUnitTests."/defaults/Default_Battle.php");
		require_once(DirectoryManager::$pathModels."/battle/BattleCommand.php");
		
		// Attempt to join the target battle
		$playerId = $request['P'];
		$battleId = $request['B'];
		$battleName = $request['Bn'];
		$return = LobbyCommand::joinStaging($request);
		$success = $return["S"];
		
		// Test Return & Forward
		If($success == 1){
			//SUCCESS - FORWARD TO STAGING!
			//=== USING UNIT TEST INSTEAD FOR NOW ================================================
			$params = array("P"=>$playerId, "B"=>$battleId, "Direction"=>"SOUTH");
			Default_Battle::addDefaultPlayerUnits($params);
			//TODO Add unit deployment phase instead of UnitTests
			//=== USING UNIT TEST INSTEAD FOR NOW ================================================
			
			self::redirect("site.php?c=GameLobby&a=joinStagingArea&B=".$battleId."&Bn=".$battleName."&P=".$playerId."&ts=".$return['ts']);
			
		}Else{
			//FAILED!
			if($success == -1){
				$error = "Unable to join! Battle is closed";
			}elseif($success == -2){
				$error = "Unable to join! Incorrect Password";
			}elseif($success == -3){
				$error = "Unable to join! Battle is Full";
			}else{
				$error = "Unable to join! Unknown Error";
			}
			//Redirect to MainLobby with Error
			self::redirect("site.php?c=GameLobby&a=joinMainLobby&E=".$error);	
		}
	}
	
	/**
	 * TODO Finish code for pollMainLobbyAction
	 *
	 * @param array $request
	 */
	public static function pollMainLobbyAction(array $request){
		require_once(DirectoryManager::$pathModels."/Lobby/LobbyCommand.php");

		//Fetch all updated Battle Status rows
		$rows = LobbyCommand::pollMainLobby($request);
		//TODO Handle rows & make return in pollMainLobbyAction
	}
	
	/**
	 * TODO Finish code for pollStagingAreaAction
	 *
	 * @param array $request
	 */
	public static function pollStagingAreaAction(array $request){
		require_once(DirectoryManager::$pathModels."/Lobby/LobbyCommand.php");

		//Fetch all updated Battle Status rows
		$rows = LobbyCommand::pollStagingArea($request);
		//TODO Handle rows & make return in pollStagingAreaAction
	}
}